import Ffmpeg from 'fluent-ffmpeg'

Ffmpeg.setFfmpegPath('/Users/huyao/home/projects/ffmpeg')
Ffmpeg.setFfprobePath('/Users/huyao/home/projects/ffprobe')

export const ffmpegHelper = {
  simpleCheck: async (videoPath: string) => {
    return new Promise<boolean>((resolve, reject) => {
      Ffmpeg.ffprobe(videoPath, (error, metadata) => {
        if (error) {
          reject(error)
        } else {
          if (metadata.streams.find(n => (n.profile as any) === 'Constrained Baseline')) {
            resolve(false)
          } else {
            resolve(true)
          }
        }
      })
    })
  },
  simpleRepair: async (videoPath: string, savePath: string, onProgress?: (percent: number) => void) => {
    return new Promise<void>(async (resolve, reject) => {
      let video = Ffmpeg(videoPath)
      video.on('progress', (e) => {
        onProgress && onProgress(e.percent)
      })
      video.on('end', function () {
        resolve()
      })
      video.on('error', function (error) {
        console.error('修复时发生错误', error)
        reject(error)
      })
      video.save(savePath)
    })
  }
}
